<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >

        <el-row >
          <el-col >
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
          </el-col>
        </el-row>



      <el-row :gutter="5">
        <el-col :span="6">
      <el-form-item label="一级类型" prop="parentTid">
        <el-input
          v-model="queryParams.parentTid"
          placeholder="请输入一级类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
          </el-col>
        <el-col :span="6">
      <el-form-item label="二级类型" prop="sonTid">
        <el-input
          v-model="queryParams.sonTid"
          placeholder="请输入二级类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
          </el-col>
        <el-col :span="5">
      <el-form-item label="比赛名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入比赛名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
          </el-col>
        </el-row>

      <el-row :gutter="5">
        <el-col :span="6">
      <el-form-item label="开始报名时间" prop="joinStart">
        <el-date-picker
          clearable
          v-model="queryParams.joinStart"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始报名时间"
          style="width: 196px"
        >
        </el-date-picker>
      </el-form-item>
          </el-col>
          <el-col :span="6">
      <el-form-item label="结束报名时间" prop="joinEnd">
        <el-date-picker
          clearable
          v-model="queryParams.joinEnd"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束报名时间"
          style="width: 196px"
        >
        </el-date-picker>
      </el-form-item>
        </el-col>
            <el-col :span="6">
      <el-form-item label="比赛开始时间" prop="raceStart">
        <el-date-picker
          clearable
          v-model="queryParams.raceStart"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择比赛开始时间"
          style="width: 196px"
        >
        </el-date-picker>
      </el-form-item>
              </el-col>
            <el-col :span="6">
      <el-form-item label="比赛结束时间" prop="raceEnd">
        <el-date-picker
          clearable
          v-model="queryParams.raceEnd"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择比赛结束时间"
          style="width: 196px"

        >
        </el-date-picker>
      </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="5">
        <el-col :span="6">
      <el-form-item label="比赛状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择比赛状态"
          clearable
          style="width: 196px"
        >
          <el-option
            v-for="dict in race_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      </el-col>
        <el-col :span="6">
      <el-form-item label="比赛级别" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择比赛级别"
          clearable
          style="width: 196px"
        >
          <el-option
            v-for="dict in race_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        </el-col>
      </el-row>


      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['buis:raceInfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buis:raceInfo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buis:raceInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['buis:raceInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="raceInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="一级类型" align="center" prop="parentName" />
      <el-table-column label="二级类型" align="center" prop="sonName" />
      <el-table-column label="比赛名称" align="center" prop="name" />
      <el-table-column label="比赛简介" align="center" prop="description" />
      <el-table-column
        label="报名开始时间"
        align="center"
        prop="joinStart"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.joinStart, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="报名结束时间"
        align="center"
        prop="joinEnd"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.joinEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="比赛开始时间"
        align="center"
        prop="raceStart"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.raceStart, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="比赛结束时间"
        align="center"
        prop="raceEnd"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.raceEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="race_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="比赛级别" align="center" prop="level">
        <template #default="scope">
          <dict-tag :options="race_level" :value="scope.row.level" />
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buis:raceInfo:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buis:raceInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改比赛信息对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body >
      <el-form
        ref="raceInfoRef"
        :model="form"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item label="一级类型" prop="parentTid" >
          <el-input v-model="form.parentTid" placeholder="请输入一级类型"   style="width: 215px" />
        </el-form-item>
        <el-form-item label="二级类型" prop="sonTid">
          <el-input v-model="form.sonTid" placeholder="请输入二级类型" style="width: 215px" />
        </el-form-item>
        <el-form-item label="比赛名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入比赛名称"  style="width: 215px"/>
        </el-form-item>
        <el-form-item label="比赛简介" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
            style="width: 500px"
          />
        </el-form-item>
        <el-form-item label="报名开始时间" prop="joinStart" label-width="110px">
          <el-date-picker
            clearable
            v-model="form.joinStart"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报名开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="joinEnd">
          <el-date-picker
            clearable
            v-model="form.joinEnd"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报名结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛开始时间" prop="raceStart">
          <el-date-picker
            clearable
            v-model="form.raceStart"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择比赛开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛结束时间" prop="raceEnd">
          <el-date-picker
            clearable
            v-model="form.raceEnd"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择比赛结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择比赛状态" style="width: 150px">
            <el-option
              v-for="dict in race_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="比赛级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择比赛级别" style="width: 150px">
            <el-option
              v-for="dict in race_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">奖项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddReward"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteReward"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="rewardList"
          :row-class-name="rowRewardIndex"
          @selection-change="handleRewardSelectionChange"
          ref="reward"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="等级" prop="rRank" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.rRank" placeholder="请输入等级" />
            </template>
          </el-table-column>
          <el-table-column label="奖品名称" prop="name" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入奖品名称" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="quantity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.quantity" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="备注说明" prop="remark" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.remark"
                placeholder="请输入备注说明"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RaceInfo">
import {
  listRaceInfo,
  getRaceInfo,
  delRaceInfo,
  addRaceInfo,
  updateRaceInfo,
} from "@/api/buis/raceInfo";

const { proxy } = getCurrentInstance();
const { race_level, race_status } = proxy.useDict("race_level", "race_status");

const raceInfoList = ref([]);
const rewardList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedReward = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    parentTid: null,
    sonTid: null,
    name: null,
    description: null,
    joinStart: null,
    joinEnd: null,
    raceStart: null,
    raceEnd: null,
    status: null,
    level: null,
  },
  rules: {
    parentTid: [
      { required: true, message: "一级类型不能为空", trigger: "blur" },
    ],
    sonTid: [{ required: true, message: "二级类型不能为空", trigger: "blur" }],
    name: [{ required: true, message: "比赛名称不能为空", trigger: "blur" }],
    description: [
      { required: true, message: "比赛简介不能为空", trigger: "blur" },
    ],
    joinStart: [
      { required: true, message: "报名开始时间不能为空", trigger: "blur" },
    ],
    joinEnd: [
      { required: true, message: "报名结束时间不能为空", trigger: "blur" },
    ],
    raceStart: [
      { required: true, message: "比赛开始时间不能为空", trigger: "blur" },
    ],
    raceEnd: [
      { required: true, message: "比赛结束时间不能为空", trigger: "blur" },
    ],
    status: [
      { required: true, message: "比赛状态不能为空", trigger: "change" },
    ],
    level: [{ required: true, message: "比赛级别不能为空", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询比赛信息列表 */
function getList() {
  loading.value = true;
  listRaceInfo(queryParams.value).then((response) => {
    raceInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentTid: null,
    sonTid: null,
    name: null,
    description: null,
    joinStart: null,
    joinEnd: null,
    raceStart: null,
    raceEnd: null,
    status: null,
    level: null,
    isDeleted: null,
    createTime: null,
    updateTime: null,
  };
  rewardList.value = [];
  proxy.resetForm("raceInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加比赛信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getRaceInfo(_id).then((response) => {
    form.value = response.data;
    rewardList.value = response.data.rewardList;
    open.value = true;
    title.value = "修改比赛信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["raceInfoRef"].validate((valid) => {
    if (valid) {
      form.value.rewardList = rewardList.value;
      if (form.value.id != null) {
        updateRaceInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRaceInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除比赛信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delRaceInfo(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 奖项序号 */
function rowRewardIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 奖项添加按钮操作 */
function handleAddReward() {
  let obj = {};
  obj.rRank = "";
  obj.name = "";
  obj.quantity = "";
  obj.remark = "";
  rewardList.value.push(obj);
}

/** 奖项删除按钮操作 */
function handleDeleteReward() {
  if (checkedReward.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的奖项数据");
  } else {
    const rewards = rewardList.value;
    const checkedRewards = checkedReward.value;
    rewardList.value = rewards.filter(function (item) {
      return checkedRewards.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleRewardSelectionChange(selection) {
  checkedReward.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "buis/raceInfo/export",
    {
      ...queryParams.value,
    },
    `raceInfo_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
